const os = require('os');
const fs = require('fs');
var os_arch; //arch x86_64
var os_org;      //Kylin
var os_type;     // Desktop Server
var os_release;// eg:2203
var os_version_id; // V10
var os_sp;//sp
var os_custom; //hwe
var pretty_name;
//检查系统架构
function getArch() {
  const arch = os.arch();
  let res = '';
  switch (arch) {
    case 'x64':
    case 'x86':
    case "x86_64": res = 'x86'; break;
    case 'arm':
    case 'arm64':
    case 'aarch64': res = 'arm'; break;
    case 'loongarch': res = 'loongarch'; break;
    default: res = "other";
  }
  return res;
}

function returnosinfo() {
  if (os_arch == 'x86' && os_org == 'kylin' && os_type == 'desktop' && os_release.includes('2203') && os_version_id == 'v10' && pretty_name.includes('sp1') && os_custom == "genhwe") {
    return "x86_kylin_desktop_v10_sp1_2203_genhwe";
  }
  if (os_arch == 'x86' && os_org == 'kylin' && os_type == 'desktop' && os_release.includes('2203') && os_version_id == 'v10' && pretty_name.includes('sp1') && os_custom == "edu") {
    return "x86_kylin_desktop_v10_sp1_2203_edu";
  }
  if (os_arch == 'x86' && os_org == 'kylin' && os_type == 'desktop' && os_release.includes('2203') && os_version_id == 'v10' && pretty_name.includes('sp1') && os_custom == "eduhwe") {
    return "x86_kylin_desktop_v10_sp1_2203_eduhwe";
  }
  if (os_arch == 'x86' && os_org == 'kylin' && os_type == 'desktop' && os_release.includes('2203') && os_version_id == 'v10' && pretty_name.includes('sp1') && os_custom == "gen") {
    return "x86_kylin_desktop_v10_sp1_2203_gen";
  }
  if (os_arch == 'x86' && os_org == 'kylin' && os_type == 'desktop' && os_release.includes('2303') && os_version_id == 'v10' && pretty_name.includes('sp1') && os_custom == "genhwe") {
    return "x86_kylin_desktop_v10_sp1_2303_genhwe";
  }
  if (os_arch == 'x86' && os_org == 'kylin' && os_type == 'desktop' && os_release.includes('2303') && os_version_id == 'v10' && pretty_name.includes('sp1') && os_custom == "gen") {
    return "x86_kylin_desktop_v10_sp1_2303_gen";
  }
  if (os_arch == 'x86' && os_org == 'kylin' && os_type == 'server' && os_release.includes('2303') && os_version_id == 'v10' && pretty_name.includes('sp1') && os_custom == "gen") {
    return "x86_kylin_server_v10_sp3_2303_gen";
  }
  if (os_arch == 'arm' && os_org == 'kylin' && os_type == 'desktop' && os_release.includes('2203') && os_version_id == 'v10' && pretty_name.includes('sp1') && os_custom == "gen") {
    return "arm_kylin_desktop_v10_sp1_2203_gen";
  }
  if (os_arch == 'arm' && os_org == 'kylin' && os_type == 'desktop' && os_release.includes('2203') && os_version_id == 'v10' && pretty_name.includes('sp1') && os_custom == "edu") {
    return "arm_kylin_desktop_v10_sp1_2203_edu";
  }
  if (os_arch == 'arm' && os_org == 'kylin' && os_type == 'desktop' && os_release.includes('2303') && os_version_id == 'v10' && pretty_name.includes('sp1') && os_custom == "gen") {
    return "arm_kylin_desktop_v10_sp1_2303_gen";
  }
  if (os_arch == 'arm' && os_org == 'kylin' && os_type == 'server' && os_release.includes('2303') && os_version_id == 'v10' && pretty_name.includes('sp3') && os_custom == "gen") {
    return "arm_kylin_server_v10_sp3_2303_gen";
  }
  if (os_arch == 'loongarch' && os_org == 'kylin' && os_type == 'desktop' && os_release.includes('2203') && os_version_id == 'v10' && pretty_name.includes('sp1') && os_custom == "gen") {
    return "loongarch_kylin_desktop_v10_sp1_2203_gen";
  }
  if (os_arch == 'loongarch' && os_org == 'kylin' && os_type == 'desktop' && os_release.includes('2303') && os_version_id == 'v10' && pretty_name.includes('sp1') && os_custom == "gen") {
    return "loongarch_kylin_desktop_v10_sp1_2303_gen";
  }
  if (os_arch == 'loongarch' && os_org == 'kylin' && os_type == 'server' && os_release.includes('2303') && os_version_id == 'v10' && pretty_name.includes('sp3') && os_custom == "gen") {
    return "loongarch_kylin_server_v10_sp3_2303_gen";
  }
  if (os_arch == 'x86' && os_org == 'openkylin' && os_type == 'desktop' && os_custom == "yangtze") {
    return "x86_openkylin_desktop_yangtze";
  }
  if (os_arch == 'x86' && os_org == 'openkylin' && os_type == 'desktop' && os_custom == "nile") {
    return "x86_openkylin_desktop_nile";
  }
  return "undefined"
}

function getosinfo() {
  const kyinfoPath = '/etc/.kyinfo';
  const osReleasePath = '/etc/os-release';
  //1、架构
  //2、是否是麒麟系统
  //3、是桌面系统还是服务器系统
  //4、是否是V10
  //5、是否带sp，是sp几
  //6、小版本号
  //7、定制版字符

  var dist_id;
  var os_info
  os_arch = getArch();
  /*分类型进行处理
  1.存在.kyinfo的系统 ，取值dist_id 中 架构之前的信息
  2.不在.kyinfo的系统，但是存在os-release  的kylin系统
  3.不是kylin的系统，但是存在os-release
  目前可以先做第一和第二种分类
  */
  let data;
  let releasedata;
  if (fs.existsSync(kyinfoPath) && fs.existsSync(osReleasePath)) {
    os_org = "kylin";
    try {
      let milestone;
      let dist_id;
      let tmpstr;
      let version_id;
      data = fs.readFileSync(kyinfoPath, 'utf8');
      releasedata = fs.readFileSync(osReleasePath, 'utf8');
      // 使用 split 方法将文件内容分割成行数组
      const lines = data.split('\n');
      // 遍历行数组 获取系统架构
      lines.forEach((line) => {
        if (line.startsWith("milestone=")) {
          milestone = line.slice(10).toLocaleLowerCase();
        } else if (line.startsWith("dist_id=")) {
          dist_id = line.slice(8).toLocaleLowerCase();
        }
      });//end lines

      const releasedatalines = releasedata.split('\n');
      releasedatalines.forEach((line) => {
        if (line.startsWith("PRETTY_NAME=")) {
          pretty_name = line.slice(12).toLocaleLowerCase().replace(/"/g, '');
        } else if (line.startsWith("VERSION_ID=")) {
          version_id = line.slice(11).toLocaleLowerCase().replace(/"/g, '');
        }
      });
      if (version_id) {
        os_version_id = version_id;
      }
      os_type = dist_id.includes('desktop') ? "desktop" : "server";
      if (milestone.indexOf('release')) {
        os_release = milestone.slice(milestone.indexOf('release') + 8);
      }

      if (dist_id.includes('edu')) {
        os_custom = 'edu';
      } else {
        os_custom = 'gen';
      }
      if (dist_id.includes('hwe')) {
        os_custom += 'hwe';
      }
      if (dist_id.includes('gfb')) {
        l
        os_custom += 'gfb'
      }
    } catch (err) {
      os_info = "";
    }
  }else if(fs.existsSync(osReleasePath)){
    // os_org = "kylin";
    try {
      let milestone;
      let dist_id;
      let tmpstr;
      let version_id;
      releasedata = fs.readFileSync(osReleasePath, 'utf8');
      const releasedatalines = releasedata.split('\n');
      releasedatalines.forEach((line) => {
        if (line.startsWith("PRETTY_NAME=")) {
          pretty_name = line.slice(12).toLocaleLowerCase().replace(/"/g, '');
        } else if (line.startsWith("VERSION_ID=")) {
          version_id = line.slice(11).toLocaleLowerCase().replace(/"/g, '');
        }else if (line.startsWith("ID=")) {
          os_org = line.slice(3).toLocaleLowerCase().replace(/"/g, '');
        }else if (line.startsWith("VERSION_CODENAME=")) {
          os_custom = line.slice(17).toLocaleLowerCase().replace(/"/g, '');
        }
      });
      if (version_id) {
        os_version_id = version_id;
      }
      if(pretty_name.includes('openkylin'))
         os_type ="desktop";
    } catch (err) {
      os_info = "";
    }
  }
  os_info = returnosinfo();
  return os_info;
}

module.exports = { getosinfo }

